$(function () {
	var lastsel;
    $("#jqGrid").jqGrid({
        url: baseURL + 'oa/oaplanemployee/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 15, key: true,editable: false,hidden:true },
			{ label: '排序', name: 'sort', index: 'sort', width: 15, editable: true},
			/*{ label: '员工唯一标识ID', name: 'userid', index: 'userid', width: 80 }, */			
			{ label: '成员名称', name: 'name', index: 'name', width: 80 }, 			
			{ label: '是否值班', name: 'active', index: 'active', width: 80 , formatter: function(value, options, row){
				return value === 1 ? 
					'<span class="label label-success">是</span>' : 
					'<span class="label label-danger">否</span>';
			}},
			{ label: '最近同步时间', name: 'updateTime', index: 'update_time', width: 80 }		
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        editurl : baseURL + 'oa/oaplanemployee/updateSort',
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        },
        ondblClickRow : function(id) {
        	if (id && id !== lastsel) {
        		$("#jqGrid").jqGrid('restoreRow', lastsel);
        		$("#jqGrid").jqGrid('editRow', id, true);
	            lastsel = id;
	        }
        	//原选中行ID
            //var oldSelectRowId = $("#selectRowId").val();
            //if (oldSelectRowId != null && oldSelectRowId != "" && oldSelectRowId.length > 0) {
               // $("#fieldGrid").jqGrid('saveRow', oldSelectRowId);//保存上一行
           // }

            //当前选中行
            //$("#selectRowId").val(id);//临时存储当前选中行
            //$("#fieldGrid").jqGrid('editRow', id);
            //$("#jqGridPager").jqGrid('editRow', id, { keys: true, focusField: 1 });

            //$("#jqGridPager").setColProp("sort",{editable:true});
            //$("#jqGridPager").editRow(15,true);
        	//jQuery("#jqGridPager").jqGrid('editRow', 15);
            
	    }
        
        
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		oaPlanEmployee: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			
			vm.showList = false;
			vm.title = "新增";
			vm.oaPlanEmployee = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.oaPlanEmployee.id == null ? "oa/oaplanemployee/save" : "oa/oaplanemployee/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.oaPlanEmployee),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "oa/oaplanemployee/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		setActive: function (event) {
			var ids = getSelectedRows();
			
			if(ids == null){
				return ;
			}
			console.log("==" + ids);
			
			$.ajax({
				type: "POST",
			    url: baseURL + "oa/oaplanemployee/setActive",
                contentType: "application/json",
                data: JSON.stringify(ids),
			    success: function(r){
					if(r.code == 0){
						alert('设置值班成功', function(index){
							$("#jqGrid").trigger("reloadGrid");
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		cancelActive: function (event) {
			var ids = getSelectedRows();
			
			if(ids == null){
				return ;
			}
			console.log( "==" + ids);
			
			$.ajax({
				type: "POST",
			    url: baseURL + "oa/oaplanemployee/cancelActive",
                contentType: "application/json",
                data: JSON.stringify(ids),
			    success: function(r){
					if(r.code == 0){
						alert('取消值班成功', function(index){
							$("#jqGrid").trigger("reloadGrid");
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		updateEmployeeList: function (event) {
			confirm('确定同步更新员工吗？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "oa/oaplanemployee/updateEmployeeList",
                    contentType: "application/json",
				    //data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('钉钉员工更新成功!', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "oa/oaplanemployee/info/"+id, function(r){
                vm.oaPlanEmployee = r.oaPlanEmployee;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});